/// <remarks>
/// GetLatest Form
/// Update all files under the given local path
/// 
/// Author: Louis-Philippe Lebouthillier, Matricis Informatique Inc.
/// </remarks>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.TeamFoundation.VersionControl.Client;
using VcLib;

namespace VcGUI
{
    public partial class GetLatestFrm : Form
    {
        private Logger logger = Logger.Instance;
        private VcLib.TFSFacade VcUtil = VcLib.TFSFacade.Instance;

        public GetLatestFrm(string project, string folderPath)
        {
            InitializeComponent();
            this.txtProject.Text = project;
            this.txtFolder.Text = folderPath;
            this.AcceptButton = this.btnOK;
            this.CancelButton = this.btnCancel;
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                GetStatus status = VcUtil.GetLastestVersion(this.txtFolder.Text, false);
                if (status.NumConflicts > 0)
                {
                    string resolved;
                    bool allConflictsResolved = true;
                    this.txtResult.AppendText(status.NumConflicts.ToString() + " Conflicts detected" + '\n');

                    this.logger.Info("Updating");

                    Conflict[] conflicts = VcUtil.GetConflicts(this.txtFolder.Text);
                    foreach (Conflict conflict in conflicts)
                    {
                        if (conflict.IsResolved)
                            resolved = "Resolved";
                        else
                        {
                            resolved = "Not resolved";
                            allConflictsResolved = false;
                        }

                        this.txtResult.AppendText(conflict.TargetLocalItem + " Conflict " + resolved + '\n');
                    }
                    if (allConflictsResolved)
                        this.logger.Info("Update Successful");
                    else
                        this.logger.ServerInfo("Update Canceled, resolve the conflict(s) first");
                    return;
                }

                if (status.GetFailures().Length == 0)
                    this.txtResult.AppendText("No conflict");

                if (status.NoActionNeeded == true)
                {
                    DialogResult result = MessageBox.Show("You have the latest version. Do you want to force overwrite on every file(s)",
                                "No action needed", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                    {
                        logger.DebugInfo("Start overwrite every file(s)");
                        status = VcUtil.GetLastestVersion(this.txtFolder.Text, true);
                        this.logger.Info("Update Successful");
                    }
                }
                else
                {
                    Failure[] failures = status.GetFailures();
                    // write Failures info
                    foreach (Failure failure in failures)
                    {
                        this.txtResult.AppendText(failure.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Error(ex.Message);
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}